<?php
define('TIMER_START', microtime(TRUE));

ini_set('display_errors', 'On');
error_reporting(E_ALL);

//define root path of application
define('BASEPATH', dirname(dirname(__FILE__)));

define('NOCACHE', (isset($_GET['nocache']) ? TRUE : FALSE));

require_once('./includes/config.inc.php');
require_once('./lib/include_dir.function.php');
//include all the helpers and functions
include_dir('./lib');

session_set_cookie_params(7 * 24 * 3600);
session_start();

$open_pages = array(
    'login.php',
    'register.php',
    'forgotpassword.php',
    'resetpassword.php',
    'news.php',
);

$get = $_SERVER['REQUEST_URI'];
if(strpos($get, '?')) {
    $get = substr($get, 0, strpos($get, '?'));
}
$msgMgr = new Msg();
if(!isset($_SESSION['user']) && !in_array(basename($get), $open_pages)) {
    if(basename($get) != '') {
        $_SESSION['msg'] = $msgMgr->get('login-required-error');
        $_SESSION['post_login'] = $_SERVER['REQUEST_URI'];
    }
    header('Location: /login.php');
    exit();
}

$userMgr = new User();
$diffMgr = new Difficulty();
$colourMgr = new Colour();
$locationMgr = new Location();
$routeMgr = new Route();
$authorMgr = new Author();
$checkinMgr = new Checkin();
$templateMgr = new Template();
$langMgr = new Language();

if(!isset($_GET['rss'])) {
   //set colours css rules
   $colour_codes = $colourMgr->getAll();
   $colour_css = array();
   require_once('./lib/lumdiff.function.php');
   foreach($colour_codes as $row) {
       if(lumdiff($row['code'], '#000000') < 4) {
           //make text colour white for darker background colour
           $colour_css[] = 'span.' . $row['name'] . ' { background-color: ' . $row['code'] . '; border: 1px solid black; color: white; }';
       } else {
           $colour_css[] = 'span.' . $row['name'] . ' { background-color: ' . $row['code'] . '; border: 1px solid black; color: black; }';
       }
   }

    $templateMgr
        ->__set('colour_css', implode("\n", $colour_css))
        ->__set('link_news', $langMgr->get('link_news'))
        ->__set('link_submit_feedback', $langMgr->get('link_submit_feedback'))
        ->__set('avatar_url', (isset($_SESSION['user']['avatar']) ? $_SESSION['user']['avatar'] : ''))
        ->loadBaseView('header');
}
?>
